public class HasPathSum {
    // 二叉树中和为某一值的路径
    // https://www.nowcoder.com/practice/508378c0823c423baa723ce448cbfd0c?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D295
    public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        return dfs(root, 0, sum);
    }

    private boolean dfs(TreeNode root, int sum, int target) {
        if (root == null) {
            return false;
        }
        sum += root.val;
        if (root.left == null && root.right == null) {
            if (sum == target) {
                return true;
            }
        }
        boolean leftResult = dfs(root.left, sum, target);
        if (leftResult) {
            return true;
        }
        return dfs(root.right, sum, target);
    }
}
